/*
 * Copyright (C) 2012 rothens
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package rpg.util;

import java.io.File;
import java.util.HashMap;
import rpg.Game;
import rpg.Maps;

/**
 *
 * @author rothens
 */
public class BinaryMapHandler {

	public HashMap<Character, Integer> ints = new HashMap<Character, Integer>();
	public HashMap<Integer, Character> landscape = new HashMap<Integer, Character>();
	public HashMap<Integer, Character> object = new HashMap<Integer, Character>();
	public HashMap<Integer, Character> entities = new HashMap<Integer, Character>();

	public BinaryMapHandler() {
		ints.put('.', 0);
		ints.put('1', 1);
		ints.put('2', 2);
		ints.put('3', 3);
		ints.put('4', 4);
		ints.put('5', 5);
		ints.put('6', 6);
		ints.put('7', 7);
		/*
		 * ---------
		 */
		ints.put('~', 0);
		ints.put('g', 1);
		ints.put('d', 2);
		ints.put('s', 3);
		ints.put('l', 4);
		/*
		 * ---------
		 */
		ints.put('z', 65535);
		ints.put('*', 1);
		ints.put('c', 2);
		ints.put('r', 3);
		/*
		 * ---------
		 */
		object.put(0, '.');
		object.put(1, '1');
		object.put(2, '2');
		object.put(3, '3');
		object.put(4, '4');
		object.put(5, '5');
		object.put(6, '6');
		object.put(7, '7');

		/*
		 * ---------
		 */
		landscape.put(0, '~');
		landscape.put(1, 'g');
		landscape.put(2, 'd');
		landscape.put(3, 's');
		landscape.put(4, 'l');
		/*
		 * ---------
		 */
		entities.put(65535, 'z');
		entities.put(0, '.');
		entities.put(1, '*');
		entities.put(2, 'c');
		entities.put(3, 'r');
	}

	public void convertMap(String name, int width, int height, String... layers) {
		int size = width * height;
		BinaryWriter bw = new BinaryWriter(new File("maps/" + name + ".map"));

		bw.writeTwoByte(width);
		bw.writeTwoByte(height);

		for (int i = 0; i < layers.length; ++i) {
			int[] bdat = new int[size];
			for (int k = 0; k < size; ++k) {
				bdat[k] = 0;
			}
			int j = (size > layers[i].length()) ? layers[i].length() : size;
			for (int k = 0; k < j; ++k) {
				char ch = layers[i].charAt(k);
				Integer val = ints.get(ch);
				bdat[k] = (val == null) ? 0 : val;
			}
			bw.writeTwoBytes(bdat);
		}
		bw.close();
	}

	public Maps.Map loadBinaryMap(String name) {
		long start = System.currentTimeMillis();
		BinaryReader br = new BinaryReader(new File("maps/" + name + ".map"));
		int width = br.readTwoByte();
		int height = br.readTwoByte();
		int size = width * height;
		String[] layers = new String[5];
		for (int j = 0; j < layers.length; ++j) {
			layers[j] = "";
			for (int i = 0; i < size; ++i) {
				int b = br.readTwoByte();
				Character c = (j==0) ? landscape.get(b) : ((j==4)? entities.get(b) : object.get(b));
				layers[j] += (c == null) ? ((j == 0) ? "~" : "." ): c;
			}
		}
		System.out.println("Map loaded in: " + (System.currentTimeMillis() - start) + "ms");
		start = System.currentTimeMillis();

		Maps.Map m = new Maps().new Map(width, height, layers[0], layers[1], layers[2], layers[3], layers[4]);
		System.out.println("Map rendered in: " + (System.currentTimeMillis() - start) + "ms");
		return m;
	}
}
